import graph
import cliques
import cliques.utils

class pajekGenerator(object):
    ''' A class for generating pajek graphs '''
    def writePajek(self,a_graph,file_name):
        nodes = a_graph.nodes()
        out_file = open(file_name,'w')
        out_file.write("*vertices %d\n" % len(nodes))
        index = 1
        node_to_index = dict()
        for node in nodes:
            out_file.write("%d %s\n" % (index,node))
            node_to_index[node]=index
            index = index+1
        out_file.write("*arcs %d\n" % len(nodes))
        for node in nodes:
            node_index = node_to_index[node]
            for neighbor in a_graph.neighbors(node):
                out_file.write("%d %d\n" % (node_index,node_to_index[neighbor]))
        out_file.close()
    
    def writePajekFromGraphVizFile(self,in_file_name, out_file_name):
        a_graph = cliques.utils.import_graph(in_file_name, 'dot')
        self.writePajek(a_graph, out_file_name)

def main():
    pajeker = pajekGenerator()    
    for i in xrange(2,7):
        in_file = '..\\resources\\results\\Graph%d.dot' % i
        out_file = '..\\resources\\results\\Graph%d.net' % i        
        pajeker.writePajekFromGraphVizFile(in_file,out_file )

if __name__ == '__main__':
    main()
    